package com.example.anql.service.impl;

import com.example.anql.common.ReturnObject;
import com.example.anql.common.ReturnUtil;
import com.example.anql.dao.ShopDao;
import com.example.anql.entity.OrderList;
import com.example.anql.dao.OrderListDao;
import com.example.anql.entity.Shop;
import com.example.anql.service.OrderListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * (OrderList)表服务实现类
 *
 * @author makejava
 * @since 2022-12-10 12:46:13
 */
@Service("orderListService")
public class OrderListServiceImpl implements OrderListService {
    @Resource
    private OrderListDao orderListDao;
    @Resource
    private ShopDao shopDao;
    @Override
    public ReturnObject list(OrderList orderList) {

        PageHelper.startPage(orderList.getPage(),orderList.getSize());
        List<OrderList> listUser = orderListDao.list(orderList);
        PageInfo<OrderList> pageInfo = new PageInfo<>(listUser);
        return ReturnUtil.success(pageInfo);


    }

    @Override
    public Object orders() {
        return  orderListDao.orders();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OrderList queryById(Integer id) {
        return this.orderListDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param orderList 筛选条件
     * @return 查询结果
     */
    @Override
    public ReturnObject queryByPage(OrderList orderList ) {

        PageHelper.startPage(orderList.getPage(),orderList.getSize());
        List<OrderList> listUser = orderListDao.queryByPage(orderList);
        PageInfo<OrderList> pageInfo = new PageInfo<>(listUser);
        return ReturnUtil.success(pageInfo);
    }

    /**
     * 新增数据
     *
     * @param orderList 实例对象
     * @return 实例对象
     */
    @Override
    public OrderList insert(OrderList orderList) {
        this.orderListDao.insert(orderList);
        return orderList;
    }

    /**
     * 修改数据
     *
     * @param orderList 实例对象
     * @return 实例对象
     */
    @Override
    public OrderList update(OrderList orderList) {
//        System.out.println(orderList.getShopState()+" ffffffffffffffff");
      if ( orderList.getShopState().toString().equals("1")    ) {
//          System.out.println("getShopState =1 ");
          OrderList orderList1 = orderListDao.queryById(orderList.getId());
          Integer shopId = orderList1.getShopId();
          Shop shop = shopDao.queryById(shopId);
          Integer number = orderList1.getNumber();
          Integer tmp = shop.getNumberCount() - number;
          if (tmp >= 0){
              shop.setNumberCount(tmp );
//              System.out.println(tmp);
              shopDao.update(shop);
              this.orderListDao.update(orderList);
              return this.queryById(orderList.getId());
          }else {

              return new OrderList();
          }




      }else {

          this.orderListDao.update(orderList);
          return this.queryById(orderList.getId());

      }


    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.orderListDao.deleteById(id) > 0;
    }
}
